package com.miaosmart.config;

import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.vectorstore.SimpleVectorStore;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RagConfig {

    @Bean
    VectorStore vectorStore(EmbeddingModel embeddingModel) {
        SimpleVectorStore simpleVectorStore = SimpleVectorStore.builder(embeddingModel)
                .build();

//        try {
//            ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
//            Resource[] resources = resolver.getResources("classpath:document/*.txt");
//            List<Document> documents = new ArrayList<>();
//
//            for (Resource resource : resources) {
//                String content = FileCopyUtils.copyToString(
//                    new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8)
//                );
//
//                // 从文件名中提取文档类型（去掉.txt后缀）
//                String filename = resource.getFilename();
//                if (filename != null) {
//                    String docType = filename.substring(0, filename.length() - 4);
//
//                    documents.add(new Document(content, Map.of(
//                        "type", docType,
//                        "filename", filename
//                    )));
//                }
//            }
//
//            if (documents.isEmpty()) {
//                throw new RuntimeException("未找到任何文档文件");
//            }
//
//            simpleVectorStore.add(documents);
//        } catch (Exception e) {
//            throw new RuntimeException("无法读取文档文件: " + e.getMessage(), e);
//        }

        return simpleVectorStore;
    }
}